<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">
        当前位置：首页
      </el-breadcrumb-item>
      <el-breadcrumb-item>优惠券管理</el-breadcrumb-item>
      <el-breadcrumb-item>优惠券配置</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索区 -->
      <el-form
        ref="searchFormRef"
        :inline="true"
        :model="queryInfo.query"
        size="mini"
      >
        <el-form-item label="适用分类" prop="apply_class">
          <el-select
            v-model="queryInfo.query.apply_class"
            placeholder="请选择"
            filterable
            size="mini"
            clearable
            @change="changeApplyClass"
          >
            <el-option
              v-for="item in selectData.product_type"
              :key="item.id"
              :label="item.name + '【' + item.id + '】'"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="适用产品" prop="apply_product">
          <el-select
            v-model="queryInfo.query.apply_product"
            placeholder="请选择"
            filterable
            size="mini"
            clearable
          >
            <el-option
              v-for="item in selectData.product"
              :key="item.id"
              :label="item.name + '【' + item.id + '】'"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="适用商品" prop="apply_commodity">
          <el-select
            v-model="queryInfo.query.apply_commodity"
            placeholder="请选择"
            filterable
            size="mini"
            clearable
          >
            <el-option
              v-for="item in selectData.goods"
              :key="item.id"
              :label="item.name + '【' + item.id + '】'"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="优惠券类别" prop="category">
          <el-select
            v-model="queryInfo.query.category"
            placeholder="请选择"
            filterable
            size="mini"
            clearable
          >
            <el-option key="1" label="满减" value="1"> </el-option>
            <el-option key="2" label="折扣" value="2"> </el-option>
            <el-option key="3" label="特惠" value="3"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关键字" prop="keyword">
          <el-input
            v-model="queryInfo.query.keyword"
            placeholder="请输入优惠券名称或ID"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            class="theme-background-color"
            size="mini"
            @click="searchList"
          >
            搜索
          </el-button>
          <el-button size="mini" @click="resetSearchForm"> 重置 </el-button>
        </el-form-item>
      </el-form>

      <!-- 操作区域 -->
      <div>
        <el-button
          class="theme-background-color"
          icon="el-icon-plus"
          size="mini"
          @click="$router.push('/coupon/addCoupon')"
        >
          新增优惠券
        </el-button>
        <el-button
          icon="el-icon-download"
          size="mini"
          :loading="btnLoading"
          @click="downloadExcel"
        >
          导出数据
        </el-button>
      </div>

      <!-- 列表区域 -->
      <el-table :data="list" border stripe>
        <!-- <el-table-column label="序号" type="index"></el-table-column> -->
        <el-table-column
          label="优惠券名称【ID】"
          min-width="200px"
          max-width="400px"
        >
          <template slot-scope="scope">
            {{ scope.row.name }}【{{ scope.row.id }}】
          </template>
        </el-table-column>
        <el-table-column
          label="券分类"
          prop="classify"
          min-width="90px"
        ></el-table-column>
        <el-table-column
          label="优惠券类别"
          prop="category.name"
          min-width="90px"
        ></el-table-column>
        <el-table-column
          label="优惠券类型"
          prop="type.name"
          min-width="90px"
        ></el-table-column>
        <el-table-column
          label="面值 | 折扣 | 特惠价"
          prop="value"
          min-width="130px"
        ></el-table-column>
        <el-table-column label="使用金额" prop="amount"></el-table-column>
        <el-table-column
          label="适用分类【ID】"
          prop="apply_class_name"
          min-width="200px"
          max-width="400px"
        ></el-table-column>
        <el-table-column
          label="适用范围说明"
          prop="apply_explain"
          min-width="100px"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          label="使用区域"
          prop="area_limit_name"
          min-width="120px"
        ></el-table-column>
        <el-table-column
          label="其他说明"
          prop="explain"
          min-width="100px"
          max-width="200px"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          label="备注"
          prop="remark"
          min-width="120px"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column label="操作" width="80px" fixed="right">
          <template slot-scope="scope">
            <el-link
              type="primary"
              @click="
                $router.push({
                  path: '/coupon/editCoupon',
                  query: { id: scope.row.id }
                })
              "
            >
              编辑
            </el-link>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: {
          apply_class: '',
          apply_commodity: '',
          apply_product: '',
          category: '',
          keyword: ''
        },
        pagenum: 1,
        pagesize: 20
      },
      list: [],
      total: 0,

      selectData: [],
      btnLoading: false
    }
  },
  created() {
    this.getSelectData()
    this.getListData()
  },
  methods: {
    async getSelectData() {
      const { data: res } = await this.$http.post('admin/Data/find', {
        code: 'goods,product,product_type',
        level: 2
      })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.selectData = res.data
    },
    async changeApplyClass(val) {
      const { data: res } = await this.$http.post('admin/Data/find', {
        code: 'goods,product',
        type_code: val
      })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.selectData.goods = res.data.goods
      this.selectData.product = res.data.product
    },
    searchList() {
      this.queryInfo.pagenum = 1
      this.getListData()
    },
    resetSearchForm() {
      //   console.log(this)
      this.$refs.searchFormRef.resetFields()
      this.getListData()
    },
    async getListData() {
      // 返回数据重命名为res
      const { data: res } = await this.$http.post(
        '/admin/coupon_config/list',
        this.queryInfo
      )
      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.list = res.data.list
      this.total = res.data.total
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getListData()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getListData()
    },
    async downloadExcel() {
      this.btnLoading = true
      this.$http({
        method: 'post',
        url: '/admin/coupon_config/export',
        data: this.queryInfo.query,
        headers: {
          token: window.sessionStorage.getItem('token')
        },
        responseType: 'blob'
      })
        .then((res) => {
          this.btnLoading = false
          if (res.data.type) {
            // 文件下载
            const blob = new Blob([res.data], {
              type: 'application/vnd.ms-excel'
            })
            let link = document.createElement('a')
            link.href = URL.createObjectURL(blob)
            link.setAttribute('download', '优惠券配置数据.xlsx')
            link.click()
            link = null
            this.$message.success('导出成功')
          } else {
            // 返回json
            this.$message.warning(res.data.msg)
          }
        })
        .catch((err) => {
          this.btnLoading = false
          console.log(err)
          this.$message.error('下载失败')
        })
    }
  }
}
</script>

<style lang="less" scoped>
</style>
